package com.ruoyi.web.wuliu.service.impl;

import java.util.List;

import com.ruoyi.app.service.impl.AppUserLoginService;
import com.ruoyi.app.utils.AppUserUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.web.wuliu.domain.*;
import com.ruoyi.web.wuliu.mapper.TReviewMapper;
import com.ruoyi.web.wuliu.mapper.TUserPostMapper;
import com.ruoyi.web.wuliu.service.IAppUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.web.wuliu.mapper.TPostMapper;
import com.ruoyi.web.wuliu.service.ITPostService;

/**
 * 帖子Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-12-05
 */
@Service
public class TPostServiceImpl implements ITPostService 
{
    @Autowired
    private TPostMapper tPostMapper;

    @Autowired
    private TReviewMapper tReviewMapper;

    @Autowired
    private IAppUserService appUserService;

    @Autowired
    private TUserPostMapper tUserPostMapper;
    /**
     * 查询帖子
     * 
     * @param id 帖子主键
     * @return 帖子
     */
    @Override
    public TPost selectTPostById(Integer id)
    {
        AppUser appUser = AppUserUtils.getAppUser();
        TPost tPost = tPostMapper.selectTPostById(id);
        if (!appUser.getId().equals(tPost.getUserId())){
            //证明是浏览
            tPost.setBrowseCount(tPost.getBrowseCount()+1);
            this.updateTPost(tPost);
        }
        return tPost;
    }

    /**
     * 查询帖子列表
     * 
     * @param tPost 帖子
     * @return 帖子
     */
    @Override
    public List<TPost> selectTPostList(TPost tPost)
    {
        List<TPost> tPosts = tPostMapper.selectTPostList(tPost);
        for (int i = 0; i < tPosts.size() ; i++) {
            TPost tPost1 = tPosts.get(i);
            AppUser user = appUserService.selectAppUserById(tPost1.getUserId());
            TUserPost tUserPost = new TUserPost();
            tUserPost.setUserId(tPost1.getUserId());
            tUserPost.setPostId(Long.valueOf(tPost1.getId()));
            List<TUserPost> tUserPosts = tUserPostMapper.selectTUserPostList(tUserPost);
            if (tUserPosts != null && tUserPosts.size()>0){
                tPost1.setGive(1);
            }else {
                tPost1.setGive(2);
            }
            tPost1.setAppUser(user);
            List<TReview> list = tReviewMapper.selectTReviewByUserId(tPost1.getId());
            tPost1.setReviewCount(list.size());
        }
        return tPosts;
    }

    /**
     * 新增帖子
     * 
     * @param tPost 帖子
     * @return 结果
     */
    @Override
    public int insertTPost(TPost tPost)
    {
        tPost.setCreateTime(DateUtils.getNowDate());
        tPost.setUserId(AppUserUtils.getAppUser().getId());
        return tPostMapper.insertTPost(tPost);
    }

    /**
     * 修改帖子
     * 
     * @param tPost 帖子
     * @return 结果
     */
    @Override
    public int updateTPost(TPost tPost)
    {
        return tPostMapper.updateTPost(tPost);
    }

    /**
     * 批量删除帖子
     * 
     * @param ids 需要删除的帖子主键
     * @return 结果
     */
    @Override
    public int deleteTPostByIds(Integer[] ids)
    {
        return tPostMapper.deleteTPostByIds(ids);
    }

    /**
     * 删除帖子信息
     * 
     * @param id 帖子主键
     * @return 结果
     */
    @Override
    public int deleteTPostById(Integer id)
    {
        return tPostMapper.deleteTPostById(id);
    }

    @Override
    public int giveLike(Integer id) {
        TPost tPost = tPostMapper.selectTPostById(id);
        tPost.setGiveLike(tPost.getGiveLike()+1);
        TUserPost tUserPost = new TUserPost();
        tUserPost.setUserId(AppUserUtils.getAppUser().getId());
        tUserPost.setPostId(Long.valueOf(id));
        tUserPostMapper.insertTUserPost(tUserPost);
        return this.updateTPost(tPost);
    }

    @Override
    public TPostMeaasge getById(Integer id) {
        TPost tPost = tPostMapper.selectTPostById(id);
        AppUser user = appUserService.selectAppUserById(tPost.getUserId());
        tPost.setAppUser(user);
        tPost.setBrowseCount(tPost.getBrowseCount()+1);
        tPostMapper.updateTPost(tPost);
        List<TReview> list = tReviewMapper.selectTReviewByUserId(id);
        for (TReview tReview : list) {
            AppUser userT = appUserService.selectAppUserById(tReview.getUserId());
            tReview.setAppUser(userT);
        }
        TUserPost tUserPost = new TUserPost();
        tUserPost.setUserId(tPost.getUserId());
        tUserPost.setPostId(Long.valueOf(tPost.getId()));
        List<TUserPost> tUserPosts = tUserPostMapper.selectTUserPostList(tUserPost);
        if (tUserPosts != null && tUserPosts.size()>0){
            tPost.setGive(1);
        }else {
            tPost.setGive(2);
        }
        TPostMeaasge build = TPostMeaasge.builder().tPost(tPost).list(list).tReviewCount(list.size()).build();
        return build;
    }

    @Override
    public int cancelGiveLike(Integer id) {
        TPost tPost = tPostMapper.selectTPostById(id);
        tPost.setGiveLike(tPost.getGiveLike()-1);
        TUserPost tUserPost = new TUserPost();
        tUserPost.setUserId(AppUserUtils.getAppUser().getId());
        tUserPost.setPostId(Long.valueOf(id));
        tUserPostMapper.deleteTUserPostByUserAndPostId(tUserPost);
        return this.updateTPost(tPost);
    }

    @Override
    public int report(TReport report) {
        AppUser appUser = AppUserUtils.getAppUser();
        report.setUserId(appUser.getId());
        return tPostMapper.insertTReport(report);
    }
}
